package com.education.expenses.web.controller;

import com.education.expenses.common.dto.GridDTO;
import com.education.expenses.common.dto.ReceiptDTO;
import com.education.expenses.core.CustomUser;
import com.education.expenses.entity.Description;
import com.education.expenses.entity.Receipt;
import com.education.expenses.entity.Report;
import com.education.expenses.facade.UserFacade;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.security.Principal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/*
*
 * Created with IntelliJ IDEA.
 * User: Anton-Aleksei Ilyin
 * Date: 10.08.12
 * Time: 16:09
 * To change this template use File | Settings | File Templates.

*/

@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserFacade userFacade;


    @RequestMapping(value = "/nextweek")
    public
    @ResponseBody
    GridDTO<ReceiptDTO> getWeekReport(
            @RequestParam("rows") Integer rows,@RequestParam("page") Integer page/*Date dateReport, int pageNumber, int pageCount, Principal principal*/){
/*        Long id = null;
        if(principal instanceof CustomUser){
            id = ((CustomUser) principal).getUserId();
        }

        GridDTO<ReceiptDTO> gridDTO = userFacade.getReport(id, dateReport, pageNumber, pageCount);*/
        GridDTO<ReceiptDTO> gridDTO = new GridDTO<ReceiptDTO>();
        gridDTO.setRecordsCount(1L);
        gridDTO.setPageCount(1L);
        gridDTO.setPageNumber(1L);
        List<ReceiptDTO> list = new ArrayList<ReceiptDTO>();
        ReceiptDTO receiptDTO = new ReceiptDTO(1L, Description.MEAL, true, "f", 2L, 3L, 4L, 4L ,4L ,4L);


        list.add(receiptDTO);
        gridDTO.setRows(list);

        return gridDTO;
    }

    @RequestMapping(value = "/weeksummary", method = RequestMethod.POST)
    public
    @ResponseBody GridDTO getPrevWeekSummary(Date dateReport, Principal principal){
        Long id = null;
        if(principal instanceof CustomUser) {
            id =  ((CustomUser) principal).getUserId();
        }
        GridDTO gridDTO = userFacade.getPrevWeekSummary(id, dateReport);
        return gridDTO;
    }

    @RequestMapping(value = "/addreceipt", method = RequestMethod.POST)
    public
    @ResponseBody Boolean addReceipt(@ModelAttribute Receipt receipt, Date reportDate, Principal principal){
        Long id = null;
        if(principal instanceof CustomUser){
            id = ((CustomUser) principal).getUserId();
        }
        if(id == null){
            //to include Logger later
            return false;
        }
        Report report = userFacade.getReport(id, reportDate);
        if(report == null){
            report = new Report(reportDate);
            report.updateReport(receipt);
            return true;
        } else {
            report.updateReport(receipt);
            return true;
        }
    }
}
